package com.cxb.view;

import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cxb.R;
import com.cxb.utils.Debug;

public class WifiItemView extends LinearLayout {

	private ScanResult scanResult;
	private TextView tvSSID;
	private TextView tvStatus;
	private ImageView ivSignal;
	ConnectivityManager netWorkManager;

	public static enum WIFI_ITEM_STATUS {
		WIFI_CONNECTING, WIFI_CONNECTED, WIFI_DISCONNECT
	}

	public WifiItemView(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.wifi_item, this);
		tvSSID = (TextView) findViewById(R.id.tv_ssid);
		tvStatus = (TextView) findViewById(R.id.tv_status);
		ivSignal = (ImageView) findViewById(R.id.ivSignal);
		netWorkManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	public void setWifiResult(ScanResult scanResult, WifiManager wifiManager) {
		this.scanResult = scanResult;
		initView(wifiManager);
	}

	public void setStatus(WIFI_ITEM_STATUS status) {

		if (status == WIFI_ITEM_STATUS.WIFI_CONNECTED) {
			tvStatus.setText(R.string.connected);
			tvStatus.setTextColor(Color.YELLOW);
		} else if (status == WIFI_ITEM_STATUS.WIFI_CONNECTING) {
			tvStatus.setText(R.string.connecting);
			tvStatus.setTextColor(Color.YELLOW);
		} else if (status == WIFI_ITEM_STATUS.WIFI_DISCONNECT) {

			tvStatus.setTextColor(Color.GRAY);
		}
	}

	private void initView(WifiManager wifiManager) {
		
		tvSSID.setText(scanResult.SSID);
		String encrypt = getEncryptString(scanResult.capabilities);
		Log.i("wifi",scanResult.SSID+"  "+encrypt);
		int strength = Math.abs(scanResult.level);
		tvStatus.setTextColor(Color.GRAY);
//		if (TextUtils.isEmpty(encrypt)||"OPEN".equals(encrypt)) {// 无密码
			tvStatus.setText(R.string.wifi_access_open);
			if (strength > 100) {
				ivSignal.setImageResource(R.drawable.wifi_unlock_0);
			} else if (strength > 80) {
				ivSignal.setImageResource(R.drawable.wifi_unlock_1);

			} else if (strength > 70) {
				ivSignal.setImageResource(R.drawable.wifi_unlock_2);
			} else if (strength > 50) {
				ivSignal.setImageResource(R.drawable.wifi_unlock_3);
			} else {
				ivSignal.setImageResource(R.drawable.wifi_unlock_4);

			}
			if (TextUtils.isEmpty(encrypt)||"OPEN".equals(encrypt)) 
			{
				findViewById(R.id.ivlock).setVisibility(View.GONE);
			}
//		} else {
//			tvStatus.setText(encrypt);
//			if (strength > 100) {
//				ivSignal.setImageResource(R.drawable.wifi_lock_0);
//			} else if (strength > 80) {
//				ivSignal.setImageResource(R.drawable.wifi_lock_1);
//
//			} else if (strength > 70) {
//				ivSignal.setImageResource(R.drawable.wifi_lock_2);
//			} else if (strength > 50) {
//				ivSignal.setImageResource(R.drawable.wifi_lock_3);
//			} else {
//				ivSignal.setImageResource(R.drawable.wifi_lock_4);
//
//			}
//
//		}
		// scanResult.

		NetworkInfo.State state = netWorkManager.getNetworkInfo(
				ConnectivityManager.TYPE_WIFI).getState();
		if (state == NetworkInfo.State.CONNECTED) {
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			String connectedSSID = wifiInfo.getSSID();
			connectedSSID=connectedSSID.replace("\"", "");
			String scanSSID = scanResult.SSID;
			Debug.printf("test", "connectedSSID=" + connectedSSID + "  scanSSID="
					+ scanSSID);
			if (connectedSSID != null && !"".equals(connectedSSID)
					&& connectedSSID.equals(scanSSID)) {
				setStatus(WIFI_ITEM_STATUS.WIFI_CONNECTED);
			}
		}
	}

	public static String getEncryptString(String capability) {

		StringBuilder sb = new StringBuilder();

		if (TextUtils.isEmpty(capability))
			return null;

		if (capability.contains("WEP")) {

			sb.append("WEP");

			return sb.toString();
		}

		if (capability.contains("WPA")) {

			sb.append("WPA");

		}
		if (capability.contains("WPA2")) {

			sb.append("/");

			sb.append("WPA2");

		}

		if (capability.contains("WPS")) {

			sb.append("/");

			sb.append("WPS");

		}

		if (TextUtils.isEmpty(sb))
			return "OPEN";

		return sb.toString();
	}

}
